package com.xzp.yygh.hosp.controller.api.auth;

import com.alibaba.fastjson.JSONObject;
import com.xzp.yygh.common.exception.YyghException;
import com.xzp.yygh.common.helper.HttpRequestHelper;
import com.xzp.yygh.common.result.Result;
import com.xzp.yygh.common.result.ResultCodeEnum;
import com.xzp.yygh.common.util.MD5;
import com.xzp.yygh.hosp.service.DepartmentService;
import com.xzp.yygh.hosp.service.HospitalService;
import com.xzp.yygh.hosp.service.HospitalSetService;
import com.xzp.yygh.hosp.service.ScheduleService;
import com.xzp.yygh.model.hosp.Department;
import com.xzp.yygh.model.hosp.Schedule;
import com.xzp.yygh.vo.hosp.DepartmentQueryVo;
import com.xzp.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;


    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;


    /**
     *
     * @param request
     * @return
     */
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){
//        System.out.println("收到.........");
        Map<String, String[]> map = request.getParameterMap();
        // 类型转换
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(map);
        // 对传入的json与数据库中的数据比对
        String hoscode = (String) switchMap.get("hoscode");
        String sign = (String) switchMap.get("sign");
        // 获取数据库中的sign
        String db_signKey = hospitalSetService.getSignKey(hoscode);
        // 对db_sign加密
        String db_encrypt = MD5.encrypt(db_signKey);
        System.out.println("db_signKey: "+db_signKey);
        // 判断json数据中的sighkey与数据库中的是否一致
        if(!Objects.equals(sign, db_encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 处理logo数据, “ ” 换为 “+”
        String logoData = (String)switchMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        switchMap.put("logoData", logoData);
        // 保存到mongodb中
        hospitalService.save(switchMap);
        return Result.ok();
    }


    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        // 判断hoscode是否存在
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
//        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        if(! Objects.equals(hospitalSetService.getSignKey(hoscode),MD5.encrypt((String) paramMap.get("sign")))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 取出mongodb中的数据
        return Result.ok(hospitalService.getByHoscode(hoscode));
    }


    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验 比较客户端数据库与传入的json数据
//        if (!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        if(! Objects.equals(hospitalSetService.getSignKey(hoscode),MD5.encrypt((String) paramMap.get("sign")))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(paramMap);
        return Result.ok();
    }


    @ApiOperation(value = "获取分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //非必填
        String depcode = (String)paramMap.get("depcode");
        // 判断page limit是否存在
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
//        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        if(! Objects.equals(hospitalSetService.getSignKey(hoscode),MD5.encrypt((String) paramMap.get("sign")))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 封装条件数据
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        Page<Department> pageModel = departmentService.selectPage(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
//必填
        String depcode = (String)paramMap.get("depcode");
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验
//        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        if(! Objects.equals(hospitalSetService.getSignKey(hoscode),MD5.encrypt((String) paramMap.get("sign")))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }


    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验
//        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        if(! Objects.equals(hospitalSetService.getSignKey(hoscode),MD5.encrypt((String) paramMap.get("sign")))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }


    @ApiOperation(value = "查询排班分页列表")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");
        //非必填
        String depcode = (String)paramMap.get("depcode");
        // 判断page limit是否存在
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if(! Objects.equals(hospitalSetService.getSignKey(hoscode),MD5.encrypt((String) paramMap.get("sign")))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> schedules = scheduleService.selectPage(page, limit, scheduleQueryVo);
        return Result.ok(schedules);

    }



    @ApiOperation(value = "删除科室")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
    //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
    //必填
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
    //签名校验
//        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        if(! Objects.equals(hospitalSetService.getSignKey(hoscode),MD5.encrypt((String) paramMap.get("sign")))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }







}

